package vg.services.progress_manager;

import java.util.LinkedList;

import vg.services.progress_manager.interfaces.IProgressManager;
import vg.services.progress_manager.interfaces.IProgressTask;

public class SimpleProgressManager implements IProgressManager {
	LinkedList<IProgressTask> tasks = new LinkedList<IProgressTask>();

	public void addTask(IProgressTask task) {
		synchronized (tasks) {	
			tasks.addLast(task);	
		}
	}

	public void removeTask(IProgressTask task) {
		synchronized (tasks) {
			tasks.remove(task);
		}
	}

	public double updateProgress() {
		synchronized (tasks) {
			if (!tasks.isEmpty()) {
				IProgressTask task = tasks.getFirst();
				if(task.getValue() == task.getLength()) removeTask(task);
				return 100.0 * task.getValue() / task.getLength(); 
			}
			return 100;
		}
	}
	
	public String getTaskName(){
		synchronized (tasks) {
			if (!tasks.isEmpty())
			{
				return tasks.getFirst().getTaskName(); 
			}
			return "";
		}
	}

	@Override
	public int getTaskCount() {
		synchronized (tasks) {
			return tasks.size();	
		}		
	}


}
